package com.xiaodu.springboot.mybatis.plus;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;

/**
 * @author: dutianjie
 * @date: 2021/5/24
 * @version: v1.0.0
 * @description: 描述
 **/
public class MyGenerrator {

    public static void main(String[] args) {

        GlobalConfig globalConfig = new GlobalConfig();
        // 配置生成的文件输出目录
        globalConfig.setOutputDir(System.getProperty("user.dir") + "\\xiaodu-springboot-mybatis-plus\\src\\main\\java");
        // author
        globalConfig.setAuthor("dtj");

        globalConfig.setOpen(false);
        // 实体名称
        globalConfig.setEntityName("%sTable");
        // controller名称
        globalConfig.setControllerName("%sController");
        // service名称
        globalConfig.setServiceName("I%sService");
        // serviceImpl名称
        globalConfig.setServiceImplName("%sServiceImpl");
        // 是否开启 ar
        globalConfig.setActiveRecord(true);
        // 是否生成xml中的baseResultMap
        globalConfig.setBaseResultMap(true);
        // id生成策略
        globalConfig.setIdType(IdType.ASSIGN_ID);
        // 是否开启swagger2
        globalConfig.setSwagger2(true);

        // 数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/mytest?useSSL=false&charsetEncoding=utf8");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("123456");
        dataSourceConfig.setDriverName("com.mysql.jdbc.Driver");
        dataSourceConfig.setDbType(DbType.MYSQL);

        // 策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        // 下划线转驼峰
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        // 生成哪些表名的映射文件
        strategyConfig.setInclude(new String[]{"sys_user_role", "sys_user_post"});

        // 生成文件的包配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("com.mybatisplus.domo");

        AutoGenerator autoGenerator = new AutoGenerator();
        autoGenerator.setDataSource(dataSourceConfig);
        autoGenerator.setGlobalConfig(globalConfig);
        autoGenerator.setTemplateEngine(new VelocityTemplateEngine());
        autoGenerator.setPackageInfo(packageConfig);
        autoGenerator.setStrategy(strategyConfig);
        autoGenerator.execute();


    }
}
